﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Print_UI
{
    public partial class FrmOverviewStudentScore : CommonForm
    {
        public FrmOverviewStudentScore()
        {
            InitializeComponent();
        }
        private int iClass = -1;
        public int iStudentID = -1;
        public int iYear, iSemester, iDepartment;
        private void FrmOverviewStudentScore_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            try
            {
                // query get student impormations in Subject follow Year , Department , Semester
                var StudentInfor = from obj in Model.PRI_StudentInSubject
                                   where obj.StudentID == iStudentID && obj.DepartmentID == iDepartment && obj.SemesterID == iSemester && obj.YearID == iYear
                                   select new
                                   {
                                       Fname = obj.PRI_Student.Firstname,
                                       Lname = obj.PRI_Student.Lastname,
                                       StudentCode = obj.PRI_Student.Code,
                                       Departmentname = obj.CAT_Department.Name,
                                       DepartMentID = obj.CAT_Department.ID,
                                       ClasName = obj.CAT_Class.Name,
                                       ClassID = obj.CAT_Class.ID,
                                       SubjectCode = obj.PRI_Subjects.Code,
                                       SubjectName = obj.PRI_Subjects.SubjectName,
                                       TotalScore = obj.TotalScore,
                                       Mid = obj.MidtermScore,
                                       Attendance = obj.AttendanceScore,
                                       obj.FinalExamScore1,
                                       obj.FinalExcamScore2
                                   };
                lblStudentFullname.Text = StudentInfor.FirstOrDefault().Fname + " " + StudentInfor.FirstOrDefault().Lname;
                lblStudentCode.Text = StudentInfor.FirstOrDefault().StudentCode;
                lblDepartmentName.Text = StudentInfor.FirstOrDefault().Departmentname;
                // get Class From Department following semester and year
                var queryClass = from obj in Model.CAT_Class.Where(c => c.DepartmentID == iDepartment).OrderByDescending(c=>c.YearID)
                                 select new
                                 {
                                     obj.ID,
                                     obj.Name
                                 };
                Dictionary<int, string> dic = new Dictionary<int, string>();
                //dic.Add(-1, "--- Chọn Học Kì ---");
                foreach (var item in queryClass)
                {
                    dic.Add(item.ID, item.Name);
                }
                cboClass.Properties.ValueMember = "Key";
                cboClass.Properties.DisplayMember = "Value";
                cboClass.Properties.DataSource = dic;
                cboClass.EditValue = StudentInfor.FirstOrDefault().ClassID;
                gridControl1.DataSource = StudentInfor;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column == STT)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }
        private void cboClass_EditValueChanged(object sender, EventArgs e)
        {
            iClass = Convert.ToInt32(cboClass.EditValue);

        }
        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Thoát giao diện này?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.Close();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var query1 = from obj in Model.PRI_StudentInSubject.Where(c => c.StudentID == iStudentID && c.YearID == iYear && c.SemesterID == iSemester)
                             select obj;
                //Each student offen dulicate following subject,using loop to update class for student in Subject.
                foreach (var item in query1)
                {
                    item.ClassID = iClass;
                }
                var YearofStudent = Model.CAT_Class.Where(c => c.ID == iClass).FirstOrDefault();
                var student = Model.PRI_Student.Where(c => c.ID == iStudentID).FirstOrDefault();
                student.YearID = YearofStudent.YearID;
                var query2 = from obj1 in Model.PRI_StudentInSemester.Where(c => c.StudentID == iStudentID && c.YearID == iYear && c.SemesterID == iSemester)
                             select obj1;
                // update Class For StudentINsemester
                query2.FirstOrDefault().ClassID = iClass;
                int resulf = Model.SaveChanges();
                if (resulf > 0)
                {
                    MessageBox.Show("Cập nhật lớp thành công");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {

                if (MessageBox.Show("Cập nhật lớp thất bại", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    CommonMessage(ex.Message);
                }

            }
        }
    }
}
